package com.ccExcelUtil.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author xiaozhao
 */
public class ObjectUtils {

    public static Object getValue(Object object, String fileName) {
        try {
            Field field = object.getClass().getDeclaredField(fileName);
            field.setAccessible(true);
            return field.get(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getStrValue(Object object, String fileName) {
        return (String) getValue(object, fileName);
    }

    public static <T> void setValue(T t, String fileName, Object value) {
        try {
            Class<T> aClass = (Class<T>) t.getClass();
            Field field = aClass.getDeclaredField(fileName);
            field.setAccessible(true);
            field.set(t, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
        if (sourceList == null) {
            return null;
        }
        List<T> targetList = new ArrayList<>(sourceList.size());
        try {
            for (Object source : sourceList) {
                T targetObject = target.newInstance();
                copyProperties(source, targetObject);
                targetList.add(targetObject);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return targetList;
    }

    /**
     * 复制对象属性
     */
    public static void copyProperties(Object source, Object target) {
        if (source == null || target == null) {
            return;
        }
        
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();
        
        Field[] sourceFields = sourceClass.getDeclaredFields();
        
        for (Field sourceField : sourceFields) {
            try {
                sourceField.setAccessible(true);
                Object value = sourceField.get(source);
                
                // 尝试在目标类中找到同名字段
                Field targetField = targetClass.getDeclaredField(sourceField.getName());
                if (targetField != null && targetField.getType().equals(sourceField.getType())) {
                    targetField.setAccessible(true);
                    targetField.set(target, value);
                }
            } catch (NoSuchFieldException e) {
                // 目标类中没有对应字段，跳过
            } catch (Exception e) {
                // 其他异常，跳过
            }
        }
    }

    public static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

